package com.lj.page;

import com.lj.dao.MsgInfoDao;
import com.lj.dao.SqliteHelper;
import com.lj.domain.MsgInfo;
import com.lj.domain.ParamsInfo;
import com.lj.domain.SimpleResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by dave on 2017/12/8.
 */
public class MsgPutServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding( "UTF-8" );

        SimpleResponse sr=new SimpleResponse();
        sr.setOk(true);
        sr.setMsg("成功!");

        //取参数
        String sender=req.getParameter("sender");//消息接收者
        String receiver=req.getParameter("receiver");//消息接收者
        String content=req.getParameter("content");//消息接收者
        MsgInfo msgInfo=new MsgInfo(0,System.currentTimeMillis(),0,0,0,sender,receiver,content);
        System.out.println("msgInfo->" + msgInfo);
        SqliteHelper sh= (SqliteHelper) req.getAttribute("sh");
        if(sh!=null) {
            MsgInfoDao msgInfoDao = new MsgInfoDao(sh);
            sr.setOk(msgInfoDao.insertMsg(msgInfo));
        }else{
            sr.setOk(false);
            sr.setMsg("!");
        }
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().println(sr.toJson());
    }
}
